# -- coding: utf-8 --
# file: 120Number120Group80Line.py
# 复制剪贴板文本第一行120个数字，存为search_arr
# 复制剪贴板文本分为120组每组80行
# 统计每组中1~49出现的次数
# 找出每个search_arr元素在对应组里位置
import pyperclip as clp

# 判断输出是否正确
def inputIsOK(str):
    # 确保str是1~49之间的数字
    if str.isnumeric() and int(str) > 0 and int(str) < 50:
        return True
    else:
        return False

# 判断字符串数组是否正确
def arrIsOK(arr):
    for item in arr:
        if item == '':
            continue
        if not inputIsOK(item):
            return False
    return True

# 把aString按splits分隔符切片
def splitString(aString, splits):
    import re
    strings = re.split(splits, aString)
    return strings

# 在字符串组Strings中找出cmpString出现的次数
def time(Strings, cmpString):
    time = 0
    for s in Strings:
        if s == cmpString:
            time += 1
    return time



if __name__=='__main__':
    # 获得待查找的120个数字的字符串数组
    lines = clp.paste().split('\n')
    search_arr = splitString(lines[0], '[ \t\r\n]+')
    if not arrIsOK(search_arr):
        # print("输入的内容不是1~49之间的数字。")
        exit("输入的内容不是1~49之间的数字。")
    else:

        lines = clp.paste().split('\n')
        if len(lines) < 120*80:
            # print("")
            exit("复制的行数不够120x80行。")

        std_lst = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0,\
               0, 0, 0, 0, 0, 0, 0, 0, 0, 0,\
               0, 0, 0, 0, 0, 0, 0, 0, 0, 0,\
               0, 0, 0, 0, 0, 0, 0, 0, 0, 0,\
               0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
        result = ""
        lineNO = 1

        # 外循环loop['extrinsic']（120）次
        loop = {'extrinsic': 120, 'internal': 80}
        for x in range(0, loop['extrinsic']):
            # 将数组lst的各值置零
            lst = std_lst[:]

            # 内循环loop['internal']（80）次
            for y in range(0, loop['internal']):
                # 取出一行分解成组
                s = splitString(lines[lineNO], '[ \t\r\n]+')
                lineNO += 1

                # 从数组中找出1~49的次数记录在lst中
                for key in range(1, 50):
                    if key < 9:
                        lst[key] += time(s, '0' + str(key))
                    else:
                        lst[key] += time(s, str(key))
            
            pos = 0
            iCmp = lst[int(search_arr[x])]
            for key in range(1,50):
                if lst[key] > iCmp or (lst[key] == iCmp and key < int(search_arr[x])):
                    pos += 1

            result = result + str(pos) + '\n'
        clp.copy(result)
